﻿using System;
using System.Windows.Forms;
using OrionApplication.OrionLib;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionForm
{
    public partial class OrionMessageForm : OrionBaseDoubleForm
    {

        public OrionMessageForm() 
            : base()
        {
            dao = new DaoOrionMessage();
            InitializeComponent();
        }

        public OrionMessageForm(String Titre)
            : base(Titre)
        {
            dao = new DaoOrionMessage();
            InitializeComponent();
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    OrionMessage om = ((DaoOrionMessage)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    om = ((DaoOrionMessage)dao).Insert(om);
                    this.RempliKDgv();
                    this.FocusSur(om.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void RempliForm(OrionMessage om)
        {
            kTbIdentMessage.Text = om.IdentMessage;
            kTbMessage.Text = om.Message;
        }

        private void SauvegardeForm()
        {
            OrionMessage om = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                om = ((DaoOrionMessage)dao).Insert(om);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    om.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    om = ((DaoOrionMessage)dao).Update(om);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(om.OrionId);
        }

        private OrionMessage RempliObject()
        {
            OrionMessage retour = new OrionMessage();

            retour.IdentMessage = kTbIdentMessage.Text;
            retour.Message = kTbMessage.Text;

            return retour;
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                OrionMessage om = ((DaoOrionMessage)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(om);
            }
        }
    }
}
